package code.service.impl;


import code.models.Phrase;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import code.dao.IPhraseDAO;
import code.dao.impl.PhraseDAO;

import code.service.IPhraseService;


import java.util.List;
import java.util.Random;


@Service(value="myService")
public class PhraseService implements IPhraseService 
{
	@Autowired
	private IPhraseDAO phraseDAO=new PhraseDAO();

	@Transactional
	public Phrase getPhrase()
	{
        List<Phrase> res=phraseDAO.getAllPhrases();
        int n=res.size();
        if(n!=0)
        {
    		return res.get((new Random()).nextInt(n));        	
        }
        else
        {
    		return new Phrase(0L,"There is no phrase in DB :(", "");
        }
	}
}
